package cn.yunyichina.scheduler.jobs.service.bone.impl;

import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.BoneService;
import cn.yunyichina.provider.framework.dubbo.service.OrderService;
import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.bone.BoneDiagnoseVo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.scheduler.jobs.service.bone.BoneSeekHelpCancleService;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhuxp on 2017/12/28.
 *
 * @param
 * @Descrition
 */
@Service
@Log4j
public class BoneSeekHelpCancleServiceImpl implements BoneSeekHelpCancleService {
    @Override
    public void doExecute() {
        //求助订单 类型 95 超过一天未支付  近期5天的订单
        OrderExtVo vo =new OrderExtVo();
        vo.setOrderType((short)95);
        vo.setOrderStatus((short)110);
        vo.setEndCreatedTime(beforeNowDay(1));
        vo.setBeginCreatedTime(beforeNowDay(5));
        try {
            log.info("获取取消求助订单列表, 调用order模块, 入参request=" + JsonUtils.toJsonString(vo));
            Response orderRespone = OrderService.callUnderlineFromInternal("yyt.order.bone.order.list", JsonUtils.toJsonString(vo));
            log.info("获取取消求助订单列表, 调用order模块, 查询结果=" + JsonUtils.toJsonString(orderRespone));
            JSONObject json = JSON.parseObject(orderRespone.getResult());
            JSONArray jsonArray = JSON.parseArray(json.get("datas").toString());
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                    String orderNo = jsonObject.getString("orderNo");
                    Map<String, Object> map = new HashMap<>();
                    map.put("order_no", orderNo);
                    //求助订单状态：待缴服务费：110、已缴服务费：111、已取消：112、已完成：113
                    map.put("order_status", 112);
                    //已取消
                    map.put("update_time", DateUtils.dateToString(new Date()));
                    Response response = BaseService.callUnderlineFromInternal("yyt.base.order.update", JsonUtils.toJsonString(map));
                    if ("0".equals(response.getResultCode())) {
                        log.info("取消订单成功!订单号：" + orderNo);
                    } else {
                        log.error("取消订单号失败!" + response.getResultMessage());
                    }
                }
            }else{
                log.info("未查询到需要取消的求助订单!此次任务执行完成！"+new Date());
            }
        }catch (Exception e){
            log.error("取消求助订单定时调度任务异常"+e);
        }
    }

    private String beforeNowDay(int i) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间前5天 时间戳
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -i);
        date = calendar.getTime();
        return simpleDateFormat.format(date);
    }
}
